<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>

<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath() %>'/>
<script>
	function move(id,m) {
		$("#loadingImg").dialog("open");
		var nextId = parseInt(id)+m;
		var currentBlock = $('#block-'+id);
		var nextBlock = $('#block-'+nextId);
		
		var currentContent = currentBlock.html();
		var nextContent = nextBlock.html();
		
		var currentSortContent = currentBlock.children("td:first-child").html();
		var nextSortContent = nextBlock.children("td:first-child").html();

		nextBlock.html(currentContent);
		currentBlock.html(nextContent);

		currentBlock.children("td:first-child").html(currentSortContent);
		nextBlock.children("td:first-child").html(nextSortContent);
		
		$('#blockListForm').ajaxSubmit({
		    url: $('#blockListForm').attr('action'),
		    cache: false,
		    type: 'post',
		    async: false,
		    success: function(sortOrderResult){
			    if (!sortOrderResult) {
				    location.replace("${midContextPath}/activityPage/page/editPage.do?activityPage.id=${pageId}");
				}
			    　$("#loadingImg").dialog("close");
			}
		});
	}

	function editBlockProduct(blockId) {
		var createBlockProductDialog = $("#createBlockProductDialog");
		createBlockProductDialog.dialog({
			autoOpen : false
			,bgiframe : true
			,width : 1000
			,modal : true
		});
		
		var url = "../blockproduct/editBlockProduct.do?block.id="+blockId+"&uid="+(new Date()).getTime();
		createBlockProductDialog.load(url,function() {
			var templateId = createBlockProductDialog.children("input:hidden").val();
			var width = 0;
			switch (templateId) {
			case '1':
				width = 920;
				break;
			case '2':
				width = 940;
				break;
			case '3':
				width = 990;
				break;
			case '4':
				width = 1000;
				break;
			case '5':
				width = 1100;
				break;
			case '6':
				width = 950;
				break;
			case '7':
				width = 1040;
				break;
			case '8':
				width = 1010;
				break;
			}
			createBlockProductDialog.data("width.dialog", width); 
			createBlockProductDialog.dialog("open");
		});
	}

	function editBlockBanner(blockId) {
		var createBlockProductDialog = $("#createBlockProductDialog");
		createBlockProductDialog.dialog({
			autoOpen : false
			,bgiframe : true
			,width : 1018
			,modal : true
		});
		
		var url = "../blockbanner/editBlockBanner.do?block.id="+blockId+"&uid="+(new Date()).getTime();
		createBlockProductDialog.load(url,function() {
			var templateId = createBlockProductDialog.children("input:hidden").val();
			createBlockProductDialog.dialog("open");
		});
	}
	
	$().ready(function(){
		var createBlockDialog = $("#createBlockDialog");
		createBlockDialog.dialog({
			autoOpen : false
			,bgiframe : true
			,width : 750
			,modal : true
		});
		
		$("#addBlockButton").click(function(){
			createBlockDialog.dialog("open");
			$("#blockTitle").focus();
			return false;
		});

		$("#createBlock").click(function(){
			if (!validateBlock()) return false; 
			$("#blockAddForm").submit();
			return false;
		});

		$("#cancelCreateBlock").click(function(){
			createBlockDialog.dialog("close");
			return false;
		});

		function validateBlock() {
			if ($("#blockTitle").val() == ""){
				alert("请输入商品区块标题文字");
				$("#blockTitle").focus();
				return false;
			}
			return true;
		}
		
		$("#loadingImg").dialog({
			title: '资料载入中',
   			bgiframe: true,
   			autoOpen: false,
   			height: 150,
   			width:280,
   			modal: true
   		});	
	});

</script>
<p class="txt-L">
	<strong><span class="txt-rd">＊</span> 3. 活动商品区块：</Strong> <span
		class="l-link"><a href="#" id="addBlockButton">新增商品区块</a>
	</span>
</p>
<s:form action="block/saveBlockOrder.do" id="blockListForm" method="POST">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tb-05" id="blockList">
	<tr>
		<th width="7%">排序</th>
		<th>区块标题</th>
		<th width="10%">区块类型</th>
		<th width="10%">创建人员</th>
		<th width="15%">最后修改时间</th>
		<th width="15%">操作</th>
	</tr>
	<s:if test="blockList.size == 0">
		<tr><td colspan="6" align="center"><p>- 无资料 -</p></td></tr>
	</s:if>
	<c-rt:forEach items="${blockList}" var="block" varStatus="rows_index">
		<c-rt:set var="trClass" value="${rows_index.count % 2 == 0 ? 'bg-yl' : ''}" />
		<tr class="${trClass}" id="block-${rows_index.count}">
			<td  align="center" >
				<c:if test="${rows_index.count > 1}">
					<img src="/images/ecadmin/template/icon-up.png" width="16" height="16" style="cursor:hand" onclick="move('${block.sortOrder}',-1);"/>
				</c:if>
				<c:if test="${(rows_index.count == 1 && fn:length(blockList) > 1) || (rows_index.count > 1 && rows_index.count != fn:length(blockList))}">
					<img src="/images/ecadmin/template/icon-down.png" width="16" height="16" style="cursor:hand" onclick="move('${block.sortOrder}',1);"/>
				</c:if>
			</td>
			<td>${block.title }</td>
			<td  align="center">${block.template.name}</td>
			<td align="center">${block.createUser }</td>
			<td  align="center"><fmt:formatDate value="${block.modifyDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
			<td  align="center" id="${block.id}">
				<input type="hidden" name="blockOrders" value="${block.id }">
				<c:choose>
					<c:when test="${block.template.productImageType == '3'}">
						<a href="javascript:editBlockBanner(${block.id});" class="a-modify" >编辑</a>
					</c:when>
					<c:otherwise>
						<a href="javascript:editBlockProduct(${block.id});" class="a-modify" >编辑</a>
					</c:otherwise>
				</c:choose>
			 | <a href="../block/delBlock.do?pageId=${pageId}&block.id=${block.id}" class="a-del">删除</a></td>
		</tr>
	</c-rt:forEach>
</table>
</s:form>
<DIV id="createBlockDialog" title="选择新增商品区块种类" style="display:none;">
	<s:form action="block/addBlock.do" id="blockAddForm" method="POST">
		<p class="txt-L">
			<strong><span class="txt-rd">＊</span> 1. 商品区块标题文字：</Strong>
			<s:textfield name="block.title" id="blockTitle" size="55" />
		</p>
		<br />
		<p class="txt-L"><strong><span class="txt-rd">＊</span> 2. 选择商品区块种类：</Strong></p>
		<div class="pick-act-template">
			<c-rt:forEach items="${blockTemplates}" var="template" varStatus="rows_index">
				<label><input name="blockTemplateId" type="radio" value="${template.id}" class="ipt-radio"  <c:if test="${rows_index.count==1 }">checked</c:if> />&nbsp; ${template.name} <br /><img src="${template.iconImageFilePath}" /></label>
			</c-rt:forEach>
		</div>
		<p class="c-f l-link" align="center"><a href="#" id="createBlock">確定</a> <a href="#" id="cancelCreateBlock">取消</a></p>
		<s:hidden name="pageId"/>
	</s:form>
</DIV>

<DIV id="createBlockProductDialog" title="选择新增商品区块种类" style="display:none;"></DIV>
<div id="loadingImg" style="display:none;">
	<img src="/images/loading.gif"/>资料载入中,请稍候...
</div>  